package com.xiang.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.xiang.seemovies.R;
import com.xiang.utils.ActivityJumpUril;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

public class LaunchActivity extends Activity{
	private ImageSwitcher imgs_showpic;
	private ImageView img_tomain;
	public static myHandler myhandler;
	private int picIndex = 0;
	private Timer myTime ;
	private Boolean isTime = false;
	private int[] pic = new int[]{R.drawable.launch01, R.drawable.launch02, R.drawable.launch03};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		initView();
		imgswShow();
	}
	@Override
	protected void onResume() {
		super.onResume();
		timing();
	}
	
	private void initView() {
		setContentView(R.layout.launchactivity_layout);
		imgs_showpic = (ImageSwitcher) findViewById(R.id.launch_img_switch_show_pic);
		img_tomain = (ImageView) findViewById(R.id.launch_ima_tomain);
		
		img_tomain.setOnClickListener(new ImageView.OnClickListener() {
			@Override
			public void onClick(View v) {
				toMain();
			}
		});
		
		
		imgs_showpic.setFactory(new ViewFactory() {  
            public View makeView() {  
            	ImageView img = new ImageView(LaunchActivity.this);
            	//设置全屏
            	img.setScaleType(ImageView.ScaleType.FIT_XY);
                return img;  
            }  
        });  
		
		myhandler = new myHandler();

	}
	private void imgswShow() {
		imgs_showpic.setImageResource(pic[picIndex]);
	}
	
	private void timing() {
		myTime = new Timer(true);
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				picIndex++;
				if(picIndex >= pic.length){
					picIndex = 0;
				}
				Message msg = new Message();
				msg.arg1 = 2;
				myhandler.sendMessage(msg);
			}
		};
		
		myTime.schedule( task, 5000, 5000);
	}
	
	class myHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(msg.arg1==2){
				imgswShow();
			}
		}
	}
	@Override
	protected void onPause() {
		super.onPause();
		cancelTimer();
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		cancelTimer();
	}
	private void cancelTimer() {
		if(myTime!=null){
			myTime.cancel();
		}
	}
	
	/**
	 * 跳转到首页，并关闭本页面
	 */
	private void toMain() {
		ActivityJumpUril.launchToFindDeal(LaunchActivity.this);
		overridePendingTransition(R.anim.right_in, R.anim.left_out);
		cancelTimer();
		this.finish();
	}

}
